package com.getpebble.android.main.drawer.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.framework.adapter.PblDynamicAdapter;
import com.getpebble.android.common.framework.adapter.PblDynamicAdapter.IPblViewBinder;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.framework.developer.PblDeviceObserver.ConnectedDeviceChangeListener;
import com.getpebble.android.main.drawer.model.DrawerItem;
import com.getpebble.android.main.drawer.view.DrawerHeaderView;
import com.getpebble.android.main.drawer.view.DrawerItemView;
import com.getpebble.android.notifications.util.NotificationUtil;
import java.util.ArrayList;
import java.util.List;

public class DrawerFragment extends PblBaseFragment {
    private static final String TAG = DrawerFragment.class.getSimpleName();
    private final ConnectedDeviceChangeListener mConnectedDeviceChangeListener = new ConnectedDeviceChangeListener() {
        public void notifyConnectedDeviceChanged() {
            Activity activity = DrawerFragment.this.getActivity();
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        DrawerFragment.this.updateForConnectedDevice();
                    }
                });
            }
        }
    };
    private int mCurrentSelectedPosition = 0;
    private DrawerHeaderView mHeaderView = null;
    private OnItemClickListener mInternalOnItemClickListener = null;
    private PblDynamicAdapter<DrawerItem> mMainAdapter = null;
    private List<DrawerItem> mMainItemHolder = new ArrayList();
    private ListView mMainListView = null;
    private Bundle mSavedInstanceState = null;
    private PblDynamicAdapter<DrawerItem> mSubAdapter = null;
    private List<DrawerItem> mSubItemHolder = new ArrayList();
    private ListView mSubListView = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mSavedInstanceState = savedInstanceState;
        if (this.mSavedInstanceState != null) {
            this.mCurrentSelectedPosition = savedInstanceState.getInt("selected_navigation_drawer_position");
        }
    }

    public int getLayoutId() {
        return R.layout.fragment_drawer;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mMainListView = (ListView) container.findViewById(R.id.lv_main_list);
        this.mSubListView = (ListView) container.findViewById(R.id.lv_sub_list);
        this.mHeaderView = (DrawerHeaderView) container.findViewById(R.id.widget_drawer_header);
        ((RelativeLayout) container.findViewById(R.id.layout_drawer_background)).setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    public void onStart() {
        super.onStart();
        if (this.mMainAdapter == null) {
            this.mMainAdapter = new PblDynamicAdapter(getActivity(), R.layout.widget_drawer_list_item);
            for (DrawerItem drawerItem : this.mMainItemHolder) {
                this.mMainAdapter.add(drawerItem);
            }
            this.mMainAdapter.setViewBinder(new IPblViewBinder() {
                public boolean onBind(Object item, View view, int position) {
                    ((DrawerItemView) view).setModel((DrawerItem) item);
                    return true;
                }
            });
            this.mMainListView.setAdapter(this.mMainAdapter);
        }
        if (this.mSubAdapter == null) {
            this.mSubAdapter = new PblDynamicAdapter(getActivity(), R.layout.widget_drawer_sub_list_item);
            for (DrawerItem drawerItem2 : this.mSubItemHolder) {
                this.mSubAdapter.add(drawerItem2);
            }
            this.mSubAdapter.setViewBinder(new IPblViewBinder() {
                public boolean onBind(Object item, View view, int position) {
                    ((DrawerItemView) view).setModel((DrawerItem) item);
                    return true;
                }
            });
            this.mSubListView.setAdapter(this.mSubAdapter);
        }
    }

    public void onResume() {
        super.onResume();
        this.mHeaderView.setShowNotificationBanner(!NotificationUtil.isServiceEnabled(getActivity()));
        PebbleApplication.addConnectedDeviceChangeListener(this.mConnectedDeviceChangeListener);
        updateForConnectedDevice();
    }

    public void onPause() {
        super.onPause();
        PebbleApplication.removeConnectedDeviceChangeListener(this.mConnectedDeviceChangeListener);
    }

    public void addMainMenuItem(DrawerItem drawerItem) {
        if (this.mMainAdapter != null) {
            this.mMainAdapter.add(drawerItem);
        } else {
            this.mMainItemHolder.add(drawerItem);
        }
    }

    public void addSubMenuItem(DrawerItem drawerItem) {
        if (this.mSubAdapter != null) {
            this.mSubAdapter.add(drawerItem);
        } else {
            this.mSubItemHolder.add(drawerItem);
        }
    }

    public void clearMenuItems() {
        this.mMainItemHolder.clear();
        this.mSubItemHolder.clear();
        if (this.mMainAdapter != null) {
            this.mMainAdapter.clear();
            this.mMainAdapter.notifyDataSetChanged();
        }
        if (this.mSubAdapter != null) {
            this.mSubAdapter.clear();
            this.mSubAdapter.notifyDataSetChanged();
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mInternalOnItemClickListener = listener;
        this.mMainListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DrawerFragment.this.mCurrentSelectedPosition = position;
                if (DrawerFragment.this.mInternalOnItemClickListener != null) {
                    DrawerFragment.this.mInternalOnItemClickListener.onItemClick(parent, view, position, id);
                }
            }
        });
        this.mSubListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DrawerFragment.this.mCurrentSelectedPosition = DrawerFragment.this.mMainAdapter.getCount() + position;
                if (DrawerFragment.this.mInternalOnItemClickListener != null) {
                    DrawerFragment.this.mInternalOnItemClickListener.onItemClick(parent, view, DrawerFragment.this.mCurrentSelectedPosition, id);
                }
            }
        });
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("selected_navigation_drawer_position", this.mCurrentSelectedPosition);
    }

    private void updateForConnectedDevice() {
        setHeaderView(PebbleApplication.getConnectedDeviceRecord());
    }

    private void setHeaderView(PblDeviceRecord connectedDevice) {
        this.mHeaderView.setHeaderStatus(connectedDevice);
    }
}
